package com.wzcl.app.controller;

import com.wzcl.app.dao.model.KnowledgePoint;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.KnowledgePointDto;
import com.wzcl.app.model.vo.KnowledgePointVo;
import com.wzcl.app.service.KnowledgePointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/knowledgePoint")
@Api(value = "知识点管理controller", tags = {"知识点管理接口"})
public class KnowledgePointController{

    @Autowired
    private KnowledgePointService service;

    @ApiOperation("新增知识点")
    @PostMapping("/add")
    public RVO<KnowledgePointVo> addKnowledgePoint(@RequestBody @Valid KnowledgePointDto knowledgePointDto) {
        KnowledgePoint knowledgePoint = service.findByProperty(knowledgePointDto);
        if(knowledgePoint == null){
            KnowledgePoint newKnowledgePoint = new KnowledgePoint();
            BeanUtils.copyProperties(knowledgePointDto,newKnowledgePoint);
            knowledgePoint = service.saveKnowledgePoint(newKnowledgePoint);
        }
        return RVO.success(KnowledgePointVo.create(knowledgePoint));
    }

    @PostMapping("/update")
    @ApiOperation("修改知识点")
    public RVO<KnowledgePointVo> updateKnowledgePoint(@RequestBody @Valid KnowledgePointDto knowledgePointDto) {
        KnowledgePoint knowledgePoint = service.updateKnowledgePoint(knowledgePointDto);
        return RVO.success(KnowledgePointVo.create(knowledgePoint));
    }

    @PostMapping("/del")
    @ApiOperation("删除知识点")
    public RVO<KnowledgePointVo> delKnowledgePoint(@RequestBody KnowledgePointDto knowledgePointDto) {
        KnowledgePoint knowledgePoint = service.delKnowledgePointById(knowledgePointDto.getId());
        return RVO.success(KnowledgePointVo.create(knowledgePoint));
    }

    @PostMapping("/get")
    @ApiOperation("获得知识点")
    public RVO<KnowledgePointVo> getKnowledgePoint(@RequestBody KnowledgePointDto knowledgePointDto) {
        KnowledgePoint knowledgePoint = service.getKnowledgePointById(knowledgePointDto.getId());
        if(knowledgePoint == null){
            return RVO.success(null);
        }
        return RVO.success(KnowledgePointVo.create(knowledgePoint));
    }

    @PostMapping("/getList")
    @ApiOperation("获得知识点List")
    public RVO<List<KnowledgePointVo>> getKnowledgePointList(@RequestBody KnowledgePointDto knowledgePointDto) {
        List<KnowledgePoint> list = service.getKnowledgePointList(knowledgePointDto);
        if(list != null && list.size() > 0 ){
            return RVO.success(list.stream().map(KnowledgePointVo::create).collect(Collectors.toList()));
        }
        return RVO.success(null);
    }

    @PostMapping("/getPage")
    @ApiOperation("获得知识点Page")
    public RVO<List<KnowledgePointVo>> getKnowledgePointPage(@RequestBody KnowledgePointDto knowledgePointDto) {
        MyPage<KnowledgePoint> page = service.getKnowledgePointPage(knowledgePointDto);
        if(page != null && page.getContent().size() > 0 ){
            return RVO.success(page.getContent().stream().map(KnowledgePointVo::create).collect(Collectors.toList()),page);
        }
        return RVO.success(null);
    }

    @GetMapping("/getByTopic/{topicId}")
    @ApiOperation("获得题目的知识点")
    public RVO<List<KnowledgePointVo>> getByTopic(@PathVariable Integer topicId) {
        List<KnowledgePoint> list = service.getKnowledgePointByTopicId(topicId);
        if(list != null && list.size() > 0 ){
            return RVO.success(list.stream().map(KnowledgePointVo::create).collect(Collectors.toList()));
        }
        return RVO.success(null);
    }
}
